package com.vay.de.dang.online.ui.apply;

import android.os.Bundle;

import java.util.List;

import com.vay.de.dang.online.R;
import com.vay.de.dang.online.entiy.IncomeRequestEntity;
import com.vay.de.dang.online.entiy.PropertyRuleEntity;
import com.vay.de.dang.online.resp.RCodeTableDto;
import com.vay.de.dang.online.ui.WithBack2Activity;
import com.vay.de.dang.online.util.RequestHelper;
import com.vay.de.dang.online.util.ResponseHelper;
import com.vay.de.dang.online.util.VnUIManager;
import com.vay.de.dang.online.util.VnUtil;
import com.vay.de.dang.online.view.DialogMenuOption;
import com.vay.de.dang.online.web.BaseApi;
import com.vay.de.dang.online.web.DeviceApi;
import com.vay.de.dang.online.web.ReqCallback;
import com.vay.de.dang.online.web.ReqResult;
import com.vay.de.dang.online.web.TableApi;
import com.vay.de.dang.online.databinding.ActivityStep2Binding;
import com.vay.de.dang.online.resp.RCustomerDataDto;

public class Step2Activity extends WithBack2Activity {

    private ActivityStep2Binding binding;
    private IncomeRequestEntity incomeRequest;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityStep2Binding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        setTitleTv(getString(R.string.title_job_info));
        incomeRequest = RequestHelper.currentIncome();

        // 业务埋点
        BaseApi.execBuriedPoint(5000, 5000,
                "完善资料-职业信息", "职业信息页可见");

        // 加载view
        loadView();
        loadData();

        if (RequestHelper.needQueueBaseRule()) {
            BaseApi.baseInfoRule(new ReqCallback() {
                @Override
                public void callback(ReqResult value) {
                    if (value.isOk()) {
                        List<PropertyRuleEntity> list = (List<PropertyRuleEntity>) value.result;
                        if (VnUtil.isEmpty(list)) {
                            finish();
                        } else {
                            RequestHelper.setBaseRuleList(list);
                        }
                    } else {
                        finish();
                    }
                }
            });
        }
    }

    @Override
    public void onBackPressed() {
        // 业务埋点
        BaseApi.execBuriedPoint(5010, 5000,
                "完善资料-职业信息", "职业信息页返回");
        super.onBackPressed();
    }

    @Override
    protected void nextActivity() {

        // 公司名称
        if (!VnUtil.isRuleOk(binding.company,
                RequestHelper.findBaseRule("companyName"))) return;
        incomeRequest.setCompanyName(binding.company.getContent());

        // 公司号码
        if (!VnUtil.isRuleOk(binding.companyPhone,
                RequestHelper.findBaseRule("companyPhone"))) return;
        incomeRequest.setCompanyPhone(binding.companyPhone.getContent());

        // 职业
        if (!VnUtil.isDropList(binding.occupation, "Not empty")) return;
        // 月收入
        if (!VnUtil.isDropList(binding.monthlyIncome, "Not empty")) return;
        // 工作年限
        if (!VnUtil.isDropList(binding.yearOfJob, "Not empty")) return;
        // 第一发薪日
        if (!VnUtil.isDropList(binding.firstPayday, "Not empty")) return;

        // 业务埋点
        BaseApi.execBuriedPoint(5099, 5000, "完善资料-职业信息", "职业信息下一步");

        // 上传资料
        DeviceApi.uploadCustomerData(2, null, null,
                null, null,
                new ReqCallback(mActivity) {
                    @Override
                    public void callback(ReqResult value) {
                        if (value.isOk()) {

                            // 下一页
                            VnUIManager.nextActivity(mActivity, Step3Activity.class);

                            // 同步资料
                            RequestHelper.queryCustomerData(mActivity, false, dto -> {

                            });
                        }
                    }
                }.showDialog(true));
    }

    private void loadView() {
        // 职业
        binding.occupation.setOnItemClick(() -> {
            new DialogMenuOption(mActivity)
                    .setOnOptionItemClick(bean -> {
                        binding.occupation.setContent(bean.getLabel());
                        incomeRequest.setPost(bean.getValue());
                    })
                    .showChoice(getString(R.string.label_occupation), TableApi.OCCUPATION);
        });

        // 月收入
        binding.monthlyIncome.setOnItemClick(() -> {
            new DialogMenuOption(mActivity)
                    .setOnOptionItemClick(bean -> {
                        binding.monthlyIncome.setContent(bean.getLabel());
                        incomeRequest.setWorkIncome(VnUtil.toIntValue(bean.getValue()));
                    })
                    .showChoice(getString(R.string.label_monthly_income), TableApi.MONTHLY_INCOME);
        });

        // 工作时长
        binding.yearOfJob.setOnItemClick(() -> {
            new DialogMenuOption(mActivity)
                    .setOnOptionItemClick(bean -> {
                        binding.yearOfJob.setContent(bean.getLabel());
                        incomeRequest.setWorkTimeLength(VnUtil.toIntValue(bean.getValue()));
                    })
                    .showChoice(getString(R.string.label_year_of_job), TableApi.WORKING_YEARS);
        });

        // 第一发薪日
        binding.firstPayday.setOnItemClick(() -> {
            new DialogMenuOption(mActivity)
                    .setOnOptionItemClick(bean -> {
                        binding.firstPayday.setContent(bean.getLabel());
                        incomeRequest.setOnePayday(bean.getValue());
                    })
                    .showChoice(getString(R.string.label_payday), TableApi.PAY_DAY);
        });

        binding.company.setContent("Test Company TCL");
        binding.companyPhone.setContent("0981910990");
    }

    private void loadData() {
        RCustomerDataDto dto = ResponseHelper.customerData;
        if (dto == null) return;

        // 公司
        binding.company.setContent(dto.getCompanyName());
        binding.companyPhone.setContent(dto.getCompanyPhone());

        // 职业
        RCodeTableDto tableDto = dto.getPost();
        if (tableDto != null) {
            binding.occupation.setContent(tableDto.getLabel());
            incomeRequest.setPost(tableDto.getValue());
        }

        // 月收入
        tableDto = dto.getWorkIncome();
        if (tableDto != null) {
            binding.monthlyIncome.setContent(tableDto.getLabel());
            incomeRequest.setWorkIncome(VnUtil.toIntValue(tableDto.getValue()));
        }

        // 工作年限
        tableDto = dto.getWorkTimeLength();
        if (tableDto != null) {
            binding.yearOfJob.setContent(tableDto.getLabel());
            incomeRequest.setWorkTimeLength(VnUtil.toIntValue(tableDto.getValue()));
        }

        // 发薪日
        tableDto = dto.getOnePayday();
        if (tableDto != null) {
            binding.firstPayday.setContent(tableDto.getLabel());
            incomeRequest.setOnePayday(tableDto.getValue());
        }
    }
}
